package com.example.controller;


import com.example.entity.Goods;
import com.example.response.Result;
import com.example.service.GoodsService;
import com.example.utils.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 货品信息管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Integer warehouseId) {
        log.info("分页查询, 参数：{},{},{},{}", page, pageSize, name, warehouseId);

        PageBean pageBean = goodsService.page(page, pageSize, name, warehouseId);

        return Result.success(pageBean);
    }


    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除操作, ids:{}", ids);
        goodsService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Goods goods) {
        log.info("新增货品, goods:{}", goods);
        goodsService.save(goods);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询货品信息, id: {}", id);
        Goods goods = goodsService.getById(id);
        return Result.success(goods);
    }

    @PutMapping
    public Result update(@RequestBody Goods goods) {
        log.info("更新货品信息 : {}", goods);
        goodsService.update(goods);
        return Result.success();
    }

}
